//
//  GKZYHomeTermAreaView.m
//  GKZY_CLASS
//
//  Created by KwokChun Cheung on 15/11/26.
//  Copyright © 2015年 eenet. All rights reserved.
//

#import "GKZYHomeTermAreaView.h"
#import "GKZYHomeViewController.h"
#import "xxptmobile.h"

@interface GKZYHomeTermAreaView()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIView *navigationTermArea;
@property (nonatomic, strong) UIView *navigationTermCover;
@property (nonatomic, strong) UITableView *navigationTermTable;
@end

@implementation GKZYHomeTermAreaView{
    CGFloat cellHeightUnit;
    CGFloat termTabelHeight;
    NSMutableArray<GKZYHomeTermCell *> *termCells;
    int clickedCellIndex;
    
    NSLayoutConstraint *selfLayoutHeight;

}

-(instancetype)init{
    if (self = [super init]) {
        cellHeightUnit = 55;
        termCells = [[NSMutableArray alloc]init];
        
        self.layer.masksToBounds = YES;
        
        [self addSubview:self.navigationTermArea];
        [self.navigationTermArea mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.bottom.equalTo(self);
        }];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    if (termCells && termCells.count>0) {
        [termCells[clickedCellIndex] setChecked:YES];
    }
}

#pragma mark - Tools Methods

-(void)clickCellByRow:(NSInteger)row{
    for (int i=0; i<termCells.count; i++) {
        GKZYHomeTermCell *cell = termCells[i];
        [cell setChecked:NO];
    }
    
    clickedCellIndex = row;
    GKZYHomeTermCell *cell = termCells[clickedCellIndex];
    [cell setChecked:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.terms.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return cellHeightUnit;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellName = @"cell";
    GKZYHomeTermCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell){
        cell = [[GKZYHomeTermCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    GKZYHomeTermModel *model = self.terms[indexPath.row];
    cell.model = model;
    
    [termCells addObject:cell];
    return cell;
}

#pragma mark - UITableViewDelegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self clickCellByRow:indexPath.row];
    GKZYHomeTermModel *model = self.terms[indexPath.row];
    self.superController.currentCid = model.identity;
    self.superController.currentPage = @(1);
    self.superController.currentIndex = indexPath.row;
    self.isShowTerm = !self.isShowTerm;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self setIsShowTerm:NO];
}

#pragma mark - Setter For Property

-(void)setIsShowTerm:(BOOL)isShowTerm{
    
    if (isShowTerm && self.terms.count>0) {
        [self removeConstraint:selfLayoutHeight];
        selfLayoutHeight = [self constraintHeight:self.superview.height];
        [self addConstraint:selfLayoutHeight];
        [self layoutIfNeeded];
    }
    
    [UIView animateWithDuration:0.25f animations:^{
        [self.navigationTermTable mas_updateConstraints:^(MASConstraintMaker *make) {
            if (isShowTerm) {
                make.top.equalTo(self.navigationTermArea).offset(0);
            }else{
                make.top.equalTo(self.navigationTermArea).offset(-termTabelHeight);
            }
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished && !isShowTerm) {
            [self removeConstraint:selfLayoutHeight];
            
            //动画完结才执行回调
            if (self.cellClick) {
                GKZYHomeTermModel *model = self.terms[clickedCellIndex];
                self.cellClick(model.identity, model.title);
            }
        }
    }];
    
    _isShowTerm = isShowTerm;
}

-(void)setTerms:(NSMutableArray *)terms{
    _terms = terms ? terms : [[NSMutableArray alloc] init];
    [self.navigationTermTable mas_updateConstraints:^(MASConstraintMaker *make) {
        termTabelHeight = terms.count * cellHeightUnit;
        make.height.equalTo(@(termTabelHeight));
        make.top.equalTo(self.navigationTermArea).offset(-termTabelHeight);
    }];
    [self.navigationTermTable reloadData];
}

#pragma mark - Getter For UI

-(UIView *)navigationTermArea{
    if (!_navigationTermArea) {
        _navigationTermArea = ({
            UIView *view = [UIView new];
            view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
            
            [view addSubview:self.navigationTermTable];
            [self.navigationTermTable mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(view);
            }];
            
            view;
        });
    }
    return _navigationTermArea;
}

-(UITableView *)navigationTermTable{
    if (!_navigationTermTable) {
        _navigationTermTable = ({
            UITableView *table = [UITableView new];
            table.delegate = self;
            table.dataSource = self;
            table;
        });
    }
    return _navigationTermTable;
}

@end
